/* jQuery script file for the report/threadUpdate.jsp view */
$(function() {
	//Retrieved from server model.
	var HINT_TEXT = $('#hintText').val();
	//For delay when posting form.
	var formSubmitTimer;
	
	$('#voteComment').tbHinter({
		text : HINT_TEXT,
		class : 'hintbox'
	});
	
	//Handle tableifying of checkboxes
	$.fn.reverse = [].reverse; //steal array reverse for jquery
	$('#violationChoices td:nth-child(3n)').reverse().each(function() {
		var html = '<td>' + $(this).prev().prev().html() + '</td>';
		html += '<td>' + $(this).prev().html() + '</td>';
		html += '<td>' + $(this).html() + '</td>';
		html = '<tr>' + html + '</tr>';
		
		$('#violationChoices tbody').prepend(html);
	});
	$('#checkboxStaging').remove();
	
	//Checks for disabled input states.
	function handleButtonStates() {
		$('#post').removeAttr('disabled');
		
		var selectedViolations = $('#violationChoices :checkbox:checked');
		var foundNV = false;
		
		selectedViolations.each(function(i, violation) {
			var selectedViolation = $(this).val();
			
			if (selectedViolation == 'No Violation') {
				if (selectedViolation == 'No Violation') {
					//Unselect all other violations and disable them
					$('#violationChoices :checkbox, #optionList :checkbox')
						.attr('checked', false)
						.attr('disabled', true);
					
					$(this).attr('disabled', false);
					$(this).attr('checked', true);
					
					//Disable action buttons
					$('#actionChoices').children(':radio').attr('disabled', 'disabled');
					$('#actionChoices').children().css('color', 'gray');
					foundNV = true;
					return false;
				}
			}
		});
		
		//If no NV, re-enable everything buttons
		if (!foundNV) {
			$('#actionChoices').children(':radio').removeAttr('disabled');
			$('#violationChoices :checkbox, #optionList :checkbox').attr('disabled', false);
			$('#actionChoices').children().css('color', 'black');
		}
	}
		 
	//Disable the post your vote button if user has voted before.
	//They must change vote to re-enable it.
	function checkNewReport() {
		if ($('#hasVoted').val() == 'true') {
			$('#post').attr('disabled', 'disabled');
		}
	}
		
	//Update vote combos after ajax form submit.
	function updateCombos(data) {
		var newCombos = $('#voteCombinations', data);
		$('#voteCombinations').html(newCombos.html());
	}
		
	//Post form data: wrapped up for reusability!
	function postData(callback) {
		$('#voteForm').ajaxSubmit(function(data) {
			$('#loader').addClass('hidden');
			if (callback) {
				callback(data);
			}
			else {
				updateCombos(data);
			}
		});
	}
	
	//Start or re-start the timer.
	function initTimer() {
		$('#loader').removeClass('hidden');
		if (formSubmitTimer) {
			window.clearTimeout(formSubmitTimer);
			formSubmitTimer = undefined;
		}
		
		formSubmitTimer = window.setTimeout(postData, 2000);
	}
		
	//Bind event to submit form when either the violations are changed,
	//or if the action is changed, or if the comment is changed.
	$('#voteComment, #violationChoices input:checkbox, input:radio').change(function() {
		handleButtonStates();
		initTimer();
	});
	
	//Open more options
	$('#openMoreOptions').click(function() {
		$('#moreOptions').slideDown('fast');
	});
	
	//Close more options and submit form
	$('#closeMoreOptions').click(function() {
		handleButtonStates();
		$('#moreOptions').slideUp('fast');
		initTimer();
		return false;
	});
	
	//Handle posting of vote
	$('#post').click(function() {
		var voteComments = $('#voteComment').val();
		var selectedAction = $('#actionChoices :radio:checked');
		
		var selectedViolations = '';
		$('#violationChoices :checkbox:checked').each(function() {
			selectedViolations += $(this).val() + ', ';
		});
		
		selectedViolations = selectedViolations.substring(0, selectedViolations.length - 2);
		
		if (voteComments == HINT_TEXT) {
			voteComments = undefined;
		}
		
		if (!selectedAction) {
			selectedAction = "No action selected";
		}
		
		var vote = {
			violation : selectedViolations,
			action : selectedAction.val(),
			comments : voteComments
		};
		
		//Make sure to submit the form, just in case.
		//This should make sure the data is saved before
		//vBulletin takes over and possibly sends us 
		//to another page via quickreply.
		postData(function(data) {
			$('#post').html('Vote posted!');
			$('#post, #voteComment').attr('disabled', 'disabled');
			
			updateCombos(data);
			
			//TODO: Later transform this into hosted API request.
			$.postMessage({
				widget: 'reports',
				message : 'voteUpdate',
				data : vote,
				error : function(e) { alert('error: ' + e.message); },
			});				
		});
		
		return false;
	});
		
	//Final initialization.
	handleButtonStates();
	checkNewReport();
	
});